package com.car.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * list 排序类
 * @author SHARK
 *
 * @param <T>
 */
public class ListSort<T> {
	/**
	 * 升序
	 */
	public static final int ASCORDER = 1;
	/**
	 * 降序
	 */
	public static final int DESCORDER = 2;
	
	private List<T> list = null;
	private String name;

	public ListSort(List<T> list) {
		this.list = list;
	}
	

	/**
	 * 排序方法
	 * @param name
	 * @param order
	 * @param orderType 1--string 2--long 3-- date
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<T> SortListByName(String name, int order,int orderType) {
		this.name = "get" + name.substring(0, 1).toUpperCase()
				+ name.substring(1, name.length());
		Collections.sort(list,getSorterById(order,orderType));
		return this.list;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private Comparator getSorterById(final int order, final int orderType) {
		return new Comparator() {
			public int compare(Object o1, Object o2) {
				T u1 = (T) o1;
				T u2 = (T) o2;
				Method m1 = null;
				Method m2 = null;
				try {
					m1 = u1.getClass().getMethod(name, null);// 获取list对象中又name指定的方法
					m2 = u2.getClass().getMethod(name, null);
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				int s1 = 0;
				
				if (1 == order) {
					try {
						if (1 == orderType){
							if (Null2Str(m1, u1 ,orderType).compareTo(Null2Str(m2, u2,orderType)) > 0) {
								s1 = 1;
							} else if (Null2Str(m1, u1,orderType).compareTo(Null2Str(m2, u2,orderType)) < 0) {
								s1 = -1;
							} else {
								s1 = 0;
							}
						}else if (2 == orderType){
							if (Null2Long(Null2Str(m1, u1,orderType)) - Null2Long(Null2Str(m2, u2,orderType)) > 0) {
								s1 = 1;
							} else if (Null2Long(Null2Str(m1, u1,orderType)) - Null2Long(Null2Str(m2, u2,orderType)) < 0) {
								s1 = -1;
							} else {
								s1 = 0;
							}
						}else if (3 == orderType){
							if (((Date)m2.invoke(u2, null)).getTime() - ((Date)m1.invoke(u1, null)).getTime() < 0){
								s1 = 1;
							}else if (((Date)m2.invoke(u2, null)).getTime() - ((Date)m1.invoke(u1, null)).getTime() > 0){
								s1 = -1;
							}else {
								s1 = 0;
							}
						}
						else if (4 == orderType){
							if (Null2Float(Null2Str(m1, u1 ,orderType)).compareTo(Null2Float(Null2Str(m2, u2,orderType))) > 0) {
								s1 = 1;
							} else if (Null2Float(Null2Str(m1, u1 ,orderType)).compareTo(Null2Float(Null2Str(m2, u2,orderType))) < 0) {
								s1 = -1;
							} else {
								s1 = 0;
							}
						}else if(5 == orderType){
							if (Null2Float(Null2Str(m1, u1 ,orderType)).compareTo(Null2Float(Null2Str(m2, u2,orderType))) > 0) {
								s1 = 1;
							} else if (Null2Float(Null2Str(m1, u1 ,orderType)).compareTo(Null2Float(Null2Str(m2, u2,orderType))) < 0) {
								s1 = -1;
							} else {
								s1 = 0;
							}

						}
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				} else {
					try {
						if (1 == orderType){
							if (Null2Str(m2, u2,orderType).compareTo(Null2Str(m1, u1,orderType)) > 0) {
								s1 = 1;
							} else if (Null2Str(m2, u2,orderType).compareTo(Null2Str(m1, u1,orderType)) < 0) {
								s1 = -1;
							} else {
								s1 = 0;
							}
						}else if (2 == orderType){
							if (Null2Long(Null2Str(m2, u2,orderType)) - Null2Long(Null2Str(m1, u1,orderType)) > 0) {
								s1 = 1;
							} else if (Null2Long(Null2Str(m2, u2,orderType)) - Null2Long(Null2Str(m1, u1,orderType) ) < 0) {
								s1 = -1;
							} else {
								s1 = 0;
							}
						}else if (3 == orderType){
							if (((Date)m2.invoke(u2, null)).getTime() - ((Date)m1.invoke(u1, null)).getTime() > 0){
								s1 = 1;
							}else if (((Date)m2.invoke(u2, null)).getTime() - ((Date)m1.invoke(u1, null)).getTime() < 0){
								s1 = -1;
							}else {
								s1 = 0;
							}
						}else if (4 == orderType){
							if (Null2Float(Null2Str(m1, u1 ,orderType)).compareTo(Null2Float(Null2Str(m2, u2,orderType))) < 0) {
								s1 = 1;
							} else if (Null2Float(Null2Str(m1, u1 ,orderType)).compareTo(Null2Float(Null2Str(m2, u2,orderType))) > 0) {
								s1 = -1;
							} else {
								s1 = 0;
							}
						}
						else if(5==orderType){
							if (Null2Float(Null2Str(m1, u1 ,orderType)).compareTo(Null2Float(Null2Str(m2, u2,orderType))) < 0) {
								s1 = 1;
							} else if (Null2Float(Null2Str(m1, u1 ,orderType)).compareTo(Null2Float(Null2Str(m2, u2,orderType))) > 0) {
								s1 = -1;
							} else {
								s1 = 0;
							}
						}
//						if (m2.invoke(u2, null).toString().compareTo(m1.invoke(u1, null).toString()) > 0) {
//							s1 = 1;
//						} else if (m2.invoke(u2, null).toString().compareTo(m1.invoke(u1, null).toString()) < 0) {
//							s1 = -1;
//						} else {
//							s1 = 0;
//						}
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				}
				return s1;
			}
		};
	}
	
	protected Long Null2Long(String s){
		if (null == s && "".equals(s)){
			return 0l;
		}
		return Long.parseLong(s);
	}
	
	protected Float Null2Float(String s){
		if (null == s && "".equals(s)){
			return 0f;
		}
		return Float.valueOf(s);
	}
	
	protected String Null2Str(Method m ,T t, int orderType) {
		Object obj = null;
		try {
			obj = m.invoke(t, null);
			if (null == obj){
				if (1 == orderType)
					obj = "";
				else {
					obj = "0";
				}
			}else if ("".equals(obj)){
				if (1 == orderType)
					obj = "";
				else {
					obj = "0";
				}
			}
			
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj.toString();
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}
	 
	 /**
	 *插入排序 降序
	 *@return
	 */
	 public static int[] bubbleSort(int[]arr){
	 if(arr == null || arr.length < 2){
	     return arr;
	 }
	 for(int i=1;i<arr.length;i++){
	 for(int j=i;j>0;j--){
	 if(arr[j]>arr[j-1]){
	 //TODO:
	 int temp=arr[j];
	 arr[j]=arr[j-1];
	 arr[j-1]=temp;
	 }else{
	 //接下来是无用功
	 break;
	 }
	 }
	 }
	 return arr;
	 }
}
